﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MapWindowDocking
{
    /// <summary>
    /// Represents a custom collection of favourites.
    /// Handles loading and writing favourites to and from the favourites file.
    /// </summary>
    public class FavouriteCollection
    {
        private string XMLFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MapInfo\\MapInfo\\Professional\\WindowInfoFavourties.xml";
        IEnumerable<Favourite> favs;

        public event EventHandler FavouritesUpdated;
        
        public void Add(Favourite favourite)
        {
            FavouriteXmlWriter writer = new FavouriteXmlWriter();
            writer.AddNewFavouriteToFile(favourite, XMLFile);

            // We set the internal favourite collection to null so that the item collection
            // will be loaded fresh when something tries to access it again.
            this.favs = null;

            if (this.FavouritesUpdated != null)
            {
                this.FavouritesUpdated(this, EventArgs.Empty);
            }
        }

        public IEnumerable<Favourite> Items
        {
            get
            {
                if (favs == null)
                {
                    this.favs = new List<Favourite>();
                    this.favs = new FavouriteXmlReader().GetFavouritesFromFile(XMLFile);
                }
                return this.favs;
            }
        }
    }
}
